package admin_category;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class Position_product extends ru.st.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

    @Test
  public void testUntitled2() throws Exception {
	Actions actions = new Actions(driver);
    driver.manage().window().maximize();
    driver.get(baseUrl + "admin/");
    /*driver.findElement(By.id("username")).clear();
    driver.findElement(By.id("username")).sendKeys("admin");
    driver.findElement(By.id("login")).clear();
    driver.findElement(By.id("login")).sendKeys("admin123");
    driver.findElement(By.cssSelector("input.form-button")).click();
    driver.findElement(By.cssSelector("a[title=\"close\"] > span")).click();*/
    actions.moveToElement(driver.findElement(By.xpath("//ul[@id='nav']/li[3]/a/span"))).build().perform(); 
    driver.findElement(By.xpath("//ul[@id='nav']/li[3]/ul/li[2]/a/span")).click();
 //   driver.findElement(By.id("ext-gen41")).click();
    driver.findElement(By.id("extdd-18")).click();
    TimeUnit.SECONDS.sleep(3);
    driver.findElement(By.id("category_info_tabs_products")).click();
    driver.findElement(By.xpath("(//input[@name='position'])[10]")).click();
    driver.findElement(By.xpath("(//input[@name='position'])[10]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[10]")).sendKeys("1");
    driver.findElement(By.xpath("(//input[@name='position'])[2]")).click();
    driver.findElement(By.xpath("(//input[@name='position'])[2]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[2]")).sendKeys("2");
    driver.findElement(By.xpath("(//input[@name='position'])[9]")).click();
    driver.findElement(By.xpath("(//input[@name='position'])[9]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[9]")).sendKeys("3");
    driver.findElement(By.xpath("(//input[@name='position'])[8]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[8]")).sendKeys("4");
    driver.findElement(By.xpath("(//input[@name='position'])[6]")).click();
    driver.findElement(By.xpath("(//input[@name='position'])[6]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[6]")).sendKeys("5");
    driver.findElement(By.cssSelector("input[name=\"position\"]")).click();
    driver.findElement(By.cssSelector("input[name=\"position\"]")).clear();
    driver.findElement(By.cssSelector("input[name=\"position\"]")).sendKeys("6");
    driver.findElement(By.xpath("(//input[@name='position'])[3]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[3]")).sendKeys("7");
    driver.findElement(By.xpath("(//input[@name='position'])[4]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[4]")).sendKeys("8");
    driver.findElement(By.xpath("(//input[@name='position'])[5]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[5]")).sendKeys("9");
    driver.findElement(By.xpath("(//input[@name='position'])[7]")).click();
    driver.findElement(By.xpath("(//input[@name='position'])[7]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[7]")).sendKeys("10");
    driver.findElement(By.cssSelector("button[title=\"Save Category\"]")).click();
    TimeUnit.SECONDS.sleep(3);
    driver.get(baseUrl + "furniture/test-category.html");
    assertEquals("", driver.findElement(By.cssSelector("ul.products-grid.first.odd > li.item.first > a[title=\"Simple product\"]")).getText());
    assertEquals("", driver.findElement(By.cssSelector("ul.products-grid.first.odd > li.item > a[title=\"Simple product out of stock\"]")).getText());
    assertEquals("", driver.findElement(By.cssSelector("ul.products-grid.first.odd > li.item.last > a[title=\"Simple product special price\"]")).getText());
    assertEquals("", driver.findElement(By.cssSelector("ul.products-grid.even > li.item.first > a[title=\"Simple product tier price\"]")).getText());
    assertEquals("", driver.findElement(By.cssSelector("ul.products-grid.even > li.item > a[title=\"Simple product custom options\"]")).getText());
    assertEquals("", driver.findElement(By.cssSelector("ul.products-grid.even > li.item.last > a[title=\"Simple product no image\"]")).getText());
    driver.get(baseUrl + "admin/");
    actions.moveToElement(driver.findElement(By.xpath("//ul[@id='nav']/li[3]/a/span"))).build().perform(); 
    driver.findElement(By.xpath("//ul[@id='nav']/li[3]/ul/li[2]/a/span")).click();
 //   driver.findElement(By.id("ext-gen41")).click();
    driver.findElement(By.id("extdd-18")).click();
    TimeUnit.SECONDS.sleep(3);
    driver.findElement(By.id("category_info_tabs_products")).click();
    driver.findElement(By.xpath("(//input[@name='position'])[10]")).click();
    driver.findElement(By.xpath("(//input[@name='position'])[10]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[10]")).sendKeys("");
    driver.findElement(By.xpath("(//input[@name='position'])[2]")).click();
    driver.findElement(By.xpath("(//input[@name='position'])[2]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[2]")).sendKeys("");
    driver.findElement(By.xpath("(//input[@name='position'])[9]")).click();
    driver.findElement(By.xpath("(//input[@name='position'])[9]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[9]")).sendKeys("");
    driver.findElement(By.xpath("(//input[@name='position'])[8]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[8]")).sendKeys("");
    driver.findElement(By.xpath("(//input[@name='position'])[6]")).click();
    driver.findElement(By.xpath("(//input[@name='position'])[6]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[6]")).sendKeys("");
    driver.findElement(By.cssSelector("input[name=\"position\"]")).click();
    driver.findElement(By.cssSelector("input[name=\"position\"]")).clear();
    driver.findElement(By.cssSelector("input[name=\"position\"]")).sendKeys("");
    driver.findElement(By.xpath("(//input[@name='position'])[3]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[3]")).sendKeys("");
    driver.findElement(By.xpath("(//input[@name='position'])[4]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[4]")).sendKeys("");
    driver.findElement(By.xpath("(//input[@name='position'])[5]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[5]")).sendKeys("");
    driver.findElement(By.xpath("(//input[@name='position'])[7]")).click();
    driver.findElement(By.xpath("(//input[@name='position'])[7]")).clear();
    driver.findElement(By.xpath("(//input[@name='position'])[7]")).sendKeys("");
    TimeUnit.SECONDS.sleep(3);
    driver.findElement(By.cssSelector("button[title=\"Save Category\"]")).click();
    TimeUnit.SECONDS.sleep(3);
  }

   private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  
  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
